package com.isp.common.model.exception;


import com.isp.common.model.ResultCode;
import com.isp.common.model.enums.SystemCodeEnum;

/**
 * @author Linsy
 * @version 1.0
 * @created 2024/7/21.
 */
public class APIRuntimeException extends RuntimeException implements IBizException {

    private static final long serialVersionUID = -3894567262837799526L;

    private Integer code;
    private String message;

    public APIRuntimeException(ResultCode resultCode) {
        this.code = resultCode.getRet();
        this.message = resultCode.getMsg();
    }

    public APIRuntimeException(ResultCode resultCode, String message) {
        this.code = resultCode.getRet();
        this.message = message == null ? resultCode.getMsg() : message;
    }

    public APIRuntimeException(Integer error, String message) {
        this.code = error;
        this.message = message;
    }

    public APIRuntimeException(Throwable cause) {
        super(cause);
        if (cause instanceof IBizException) {
            this.code = ((IBizException) cause).getCode();
        } else {
            this.code = SystemCodeEnum.SERVER_ERROR.getRet();
        }
        this.message =
                cause.getMessage() == null ? SystemCodeEnum.SERVER_ERROR.getMsg() : cause.getMessage();
    }

    public APIRuntimeException(String message, Throwable cause) {
        super(message, cause);
        if (cause instanceof IBizException) {
            this.code = ((IBizException) cause).getCode();
        } else {
            this.code = SystemCodeEnum.SERVER_ERROR.getRet();
        }
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
